import java.util.HashMap;

//数青蛙
//https://leetcode.cn/problems/minimum-number-of-frogs-croaking/description/
public class Test {
    public static void main(String[] args) {
        //
    }
}

//方法二：哈希映射的方式
class Solution {
    public int minNumberOfFrogs(String croaks) {
        int n = croaks.length();//蛙鸣（混合版）长度
        String  s = "croak";//一句完整蛙鸣
        int len = s.length();//一句完整蛙鸣 的长度

        //用数组模拟哈希表
        int[] hash = new int[len];
        //用一个哈希表来保存字符和下标的映射关系
        HashMap<Character, Integer> index = new HashMap<>();
        for (int i = 0; i < len; i++) {
            char ch = s.charAt(i);
            index.put(ch, i);
        }

        for(int i = 0; i < n; i++){
            int x = index.get(croaks.charAt(i));//蛙鸣的字符的下标（映射）

            if(x==0){//一句蛙叫的第一个字符
                if(hash[len-1]!=0){
                    hash[len-1]--;
                }
                hash[x]++;
                continue;
            }

            if(hash[x-1]==0){//不是第一个字符，看前驱字符个数
                return -1;
            }
            hash[x]++;
            hash[x-1]--;
        }

        for(int i = 0; i < len-1; i++){
            if(hash[i]!=0){
                return -1;
            }
        }
        return hash[len-1];

    }
}

//方法一：使用分支语句实现（switch或者if-else，都可以，这里使用switch，顺便复习一下它的用法和注意事项，比如：break）
class Solution1 {
    public int minNumberOfFrogs(String croakOfFrogs) {
        HashMap<Character, Integer> hash = new HashMap<Character, Integer>();
        for (int i = 0; i < croakOfFrogs.length(); i++) {
            char ch = croakOfFrogs.charAt(i);
            switch(ch){
                case 'c':
                    hash.put(ch, hash.getOrDefault(ch, 0) + 1);
                    int count1 = hash.get('k');
                    if(count1 != 0){
                        hash.put('k',  count1-1);
                    }

                case 'r':
                    int count2 = hash.get('c');
                    if(count2 == 0){
                        return -1;
                    }
                    hash.put(ch, hash.getOrDefault(ch, 0) + 1);
                    hash.put('c',  count2-1);

                case 'o':
                    int count3 = hash.get('r');
                    if(count3 == 0){
                        return -1;
                    }
                    hash.put('r',  count3-1);
                    hash.put(ch,  hash.getOrDefault(ch, 0) + 1);
                case 'a':
                    int count4 = hash.get('o');
                    if(count4 == 0){
                        return -1;
                    }
                    hash.put('o',  count4-1);
                    hash.put(ch,  hash.getOrDefault(ch, 0) + 1);
                case 'k':
                    int count5 = hash.get('a');
                    if(count5 == 0){
                        return -1;
                    }
                    hash.put('a',  count5-1);
                    hash.put('k',  count5-1);
            }
        }
        return hash.get('k');
    }
}
